package com.i2bt.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 
 * Title : SQLite数据库助手类
 *
 * Description : 创建数据库以及创建表
 *
 * CreatedTime : 2014年10月19日
 *
 * @author wuliao & kakaxi & yi & flame & Charl_ZH
 *
 * @version 1.0
 *
 */
public class SQLiteHelper extends SQLiteOpenHelper {
	/**
	 * 数据库名和版本号
	 */
	public static final String DB_NAME = "cache.db";
	public static final int DB_VERSION = 1;
	/**
	 * 用户的HomeBox列表
	 */
	public static final String TABLE_HOMEBOXES       = "homeboxes";
	public static final String HOMEBOXES_COLUMN_ID   = "_id";
	public static final String HOMEBOXES_COLUMN_NAME = "homebox_name";
	/**
	 * 文件表和属性列名（ID、文件名、文件大小、是否为文件）
	 */
	public static final String TABLE_FILES         = "files";
	public static final String FILES_COLUMN_ID     = "_id";
	public static final String FILES_COLUMN_NAME   = "file_name";
	public static final String FILES_COLUMN_LENGTH = "file_length";
	public static final String FILES_COLUMN_ISFILE = "is_file";
	/**
	 * 文件根目录表和属性列名（ID、名、根目录名、）
	 */
	public static final String TABLE_PARENTS       = "parents";
	public static final String PARENT_COLUMN_ID    = "_id";
	public static final String PARENT_COLUMN_FILES_ID  = "file";
	/**
	 * 创建表、操作表的SQL
	 */
	public static final String SQL_CREATE_TABLE_HOMEBOXES =
			"CREATE TABLE IF NOT EXISTS " + TABLE_HOMEBOXES + " ( "
			+ HOMEBOXES_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
			+ HOMEBOXES_COLUMN_NAME + " TEXT);";
	public static final String SQL_CREATE_TABLE_FILES     =
			"CREATE TABLE IF NOT EXISTS " + TABLE_FILES + " ( "
			+ FILES_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ FILES_COLUMN_NAME + " TEXT,"
			+ FILES_COLUMN_LENGTH + " INTEGER,"
			+ FILES_COLUMN_ISFILE + " CHAR(1)"
			+ ");";
	public static final String SQL_CREATE_TABLE_PARENTS   =
			"CREATE TABLE IF NOT EXISTS " + TABLE_PARENTS + " ( "
			+ PARENT_COLUMN_ID + " INTEGER REFERENCES " 
			+ TABLE_FILES + "(" + FILES_COLUMN_ID + "),"
			+ PARENT_COLUMN_FILES_ID + " INTEGER REFERENCES " 
			+ TABLE_FILES + "(" + FILES_COLUMN_ID + ")"
			+ ");";
	
	/**
	 * 构造方法
	 * @param context
	 */
	public SQLiteHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL(SQL_CREATE_TABLE_HOMEBOXES);
		db.execSQL(SQL_CREATE_TABLE_PARENTS);
		db.execSQL(SQL_CREATE_TABLE_FILES);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
	}
}
